#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QDesktopWidget>

#define DIR_USERDATA "/userdata"
#define DIR_HOME QStandardPaths::writableLocation(QStandardPaths::HomeLocation)

MainWindow::MainWindow(QWidget *parent)
    : QMainWindow(parent)
    , ui(new Ui::MainWindow)
{
    ui->setupUi(this);
    this->setWindowTitle("QCamera");

    QFileInfo fi(DIR_USERDATA);
    if(fi.isDir()){
        locationDir = DIR_USERDATA;
    }else {
        QFileInfo fi(DIR_HOME);
        if(fi.isDir()){
            locationDir = DIR_HOME;
        }
    }
    imageCnt = 0;

    const QRect availableGeometry = QApplication::desktop()->availableGeometry(this);
    resize(availableGeometry.width(),availableGeometry.height()-50);

    QWidget* widget = new QWidget(this);
    vlayout = new QVBoxLayout(this);
    widget->setLayout(vlayout);
    this->setCentralWidget(widget);

    combobox=new QComboBox(this);
    pushbutton_show=new QPushButton("预览照片",this);
    pushbutton_capture=new QPushButton("拍照",this);
    pushbutton_exit = new QPushButton("退出",this);
    //设置控件的ObjectName
    combobox->setObjectName("combobox");
    pushbutton_show->setObjectName("pushbutton_show");
    pushbutton_capture->setObjectName("pushbutton_capture");
    //设置控件的尺寸
    combobox->setFixedHeight(40);
    pushbutton_show->setFixedSize(QSize(120,40));
    pushbutton_capture->setFixedSize(QSize(120,40));
    pushbutton_exit->setFixedSize(120,40);
    //设置控件的字体
    QFont font;
    font.setFamily("微软雅黑");
    font.setPointSize(10);
    combobox->setFont(font);
    pushbutton_show->setFont(font);
    pushbutton_capture->setFont(font);
    //新建内层布局，并将控件添加到布局
    QHBoxLayout* hlayout = new QHBoxLayout(this);
    hlayout->addWidget(combobox);
    hlayout->addWidget(pushbutton_show);
    hlayout->addWidget(pushbutton_capture);
    hlayout->addWidget(pushbutton_exit);
    //初始化下拉框内容
    list= QCameraInfo::availableCameras();
//    for (int i = 0; i < list.size(); i++)
//        combobox->addItem(list.at(i).description());

    connect(pushbutton_show,SIGNAL(clicked(bool)),this,SLOT(showbutton_slot()));
    connect(pushbutton_capture,SIGNAL(clicked(bool)),this,SLOT(pushbutton_slot()));
    connect(pushbutton_exit, SIGNAL(clicked(bool)), this, SLOT(exitbutton_slot()));

    /*遍历相机将匹配的打开并显示*/
    for(const QCameraInfo &camerainfo : list)
    {
        if(!camerainfo.description().compare("rkisp_mainpath"))
        {

            viewfinder=new QCameraViewfinder(this);
            vlayout->addWidget(viewfinder);
            camera = new QCamera(camerainfo);
            imageCapture=new QCameraImageCapture(camera);
            camera->setViewfinder(viewfinder);
            camera->start();
            combobox->addItem(camerainfo.description());
        }
        if(!camerainfo.description().compare("USB 2.0 Camera: USB Camera"))
        {
            qDebug() << "---------------usb 3.0";
            viewfinder=new QCameraViewfinder(this);

            viewfinder->setFixedSize(availableGeometry.width(),availableGeometry.height()/3); /*设置显示区域大小*/
            vlayout->addWidget(viewfinder);
            camera = new QCamera(camerainfo);

            QList<QSize> sizes = camera->supportedViewfinderResolutions();
           qDebug() << "viewfinderResolutions sizes.len = " << sizes.length();
           foreach (QSize size, sizes) {
              qDebug() << "Resilutions size = " << size;
           }

            imageCapture=new QCameraImageCapture(camera);
            camera->setViewfinder(viewfinder);
            camera->start();
            combobox->addItem(camerainfo.description());
        }
    }
     vlayout->addLayout(hlayout);

     this->update();
}

MainWindow::~MainWindow()
{
    delete ui;
}
/**
 * @brief MainWindow::pushbutton_slot
 * 功能：拍照按钮槽函数
 */
void MainWindow::pushbutton_slot()
{
    if(camera->captureMode() == QCamera::CaptureStillImage)
    {
        if(imageCapture->isReadyForCapture())
            takeImage();
    }
}
/**
 * @brief MainWindow::takeImage
 * 功能：保存照片到指定路径
 */
void MainWindow::takeImage()
{
    QFileInfo fi;
    QString lo;

    lo = locationDir + "/" + "realCamer" + QString::number(imageCnt) + ".jpg";
    fi = QFileInfo(lo);

    while(fi.isFile()){
        imageCnt++;
        lo = locationDir + "/" + "realCamer" + QString::number(imageCnt) + ".jpg";
        fi = QFileInfo(lo);
    }

    imageCapture->capture(lo);
}
/**
 * @brief MainWindow::showbutton_slot
 * 功能：调用预览图片界面
 */
void MainWindow::showbutton_slot()
{
    showpic = new showPic();
    showpic->show();
}
/**
 * @brief MainWindow::exitbutton_slot
 * 功能：退出按键槽函数
 */
void MainWindow::exitbutton_slot()
{
    qDebug()<<"is stop";
    camera->stop();
    this->close();
}
